//
//  TopCell.m
//  WorldOfFacts_V2
//
//  Created by Demjanko Denis on 21.11.12.
//  Copyright (c) 2012 it-dimension. All rights reserved.
//

#import "TopCell.h"
#import "PhotoBox.h"
#import "UIImageView+WebCache.h"
#import "DetailFact_VC.h"

@implementation TopCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        likesBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"like-count.png"]];
        likesBackground.frame = CGRectMake(120, 69, 39, 25);
        [self.contentView addSubview:likesBackground];
        
        likeImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"likeIcon.png"]];
        likeImage.frame = CGRectMake(95, 72, 22, 20);
        [self.contentView addSubview:likeImage];
        
        countLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 68, 39, 25)];
        countLabel.backgroundColor = [UIColor clearColor];
        countLabel.font = [UIFont fontWithName:@"Helvetica" size:13.0f];
        countLabel.textAlignment = UITextAlignmentCenter;
        countLabel.textColor = [UIColor colorWithRed:58.0f/255.0f green:82.0f/255.0f blue:152.0f/255.0f alpha:1.0f];
        [self.contentView addSubview:countLabel];    
                
        photoBox = [[PhotoBox alloc] initWithFrame:CGRectMake(7, 10, 80, 80)];
        [self.contentView addSubview:photoBox];
                
        self.textLabel.shadowColor = [UIColor whiteColor];
        self.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14];
        self.textLabel.numberOfLines = 3;
        self.textLabel.highlightedTextColor = [UIColor clearColor];
        
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        separator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"comments_separator.png"]];
        separator.frame = CGRectMake(0, 98, 320, 2);
        [self.contentView addSubview:separator];
        
        accessory = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 13, 13)];
        accessory.contentMode = UIViewContentModeScaleAspectFit;
        accessory.image = [UIImage imageNamed:@"unread-indicator"];
        accessory.highlightedImage = [UIImage imageNamed:@"arrow_table"];
        self.accessoryView = accessory;
        
        badge_favorite = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"badge-favorite"]];
        badge_favorite.frame = CGRectMake(295, 0, 25, 25);
        [self.contentView addSubview:badge_favorite];
    }
    
    return self;
}

#pragma mark - Layout

-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGSize textLabelSize = [self.textLabel.text sizeWithFont:self.textLabel.font  constrainedToSize:CGSizeMake(190, 80) lineBreakMode:self.textLabel.lineBreakMode];
    
    self.textLabel.frame = CGRectMake(95, 0, 195, textLabelSize.height);    
}

#pragma mark - Data

-(void)setFact:(NSMutableDictionary*)fact{ 
    //58 82 152
    badge_favorite.hidden = ![[fact valueForKey:@"is_favorite"] intValue];
    accessory.highlighted = ![[fact valueForKey:@"is_read"] intValue];
    
    self.textLabel.text = [fact valueForKey:@"mini_description"];
    
    NSString *icon_url = [[fact valueForKey:@"icon"] stringByReplacingOccurrencesOfString:@"\"" withString:@""];
    NSString* escapedUrlString = [icon_url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [photoBox startIndicator:YES];
    
    [photoBox setImageWithURL:[NSURL URLWithString:escapedUrlString] placeholderImage:nil isScaled:YES size:CGSizeMake(160, 160)];
    
    countLabel.text = [fact valueForKey:@"likes"];
}

#pragma mark - Memory

-(void)dealloc{
    countLabel = nil;
    likesBackground = nil;
    likeImage = nil;
    separator = nil;
    accessory = nil;
    badge_favorite = nil;
    photoBox = nil;
}

@end


